package edu.mis.modules.ledger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

import edu.mis.model.Kmye;
import edu.mis.model.Voucheritem;

public class SubsidiaryLedgerController extends Controller {
	public void index() {
		render("subsidiaryLedger.html");
	}

	/**
	 * 获取明细账
	 */
	public void get() {
		Integer captionId = getParaToInt("captionId");
		String date1 = getPara("date1");
		List<Kmye> list = Kmye.dao.find("select * from kmye where captionId = " + captionId
				+ " and date_format(date,'%Y-%m')='" + date1 + "' and isInit = 0");
		int len = list.size();
		if (len == 0) {
			setAttr("success", false);
			setAttr("msg", "未查询到数据，请关账后再查!");
			renderJson();
		} else {
			JSONArray array = new JSONArray();

			// 科目余额数据
			Map<String, Object> kmye = new HashMap<String, Object>();
			// 年-月
			java.sql.Date date = list.get(0).get("date");
			java.text.SimpleDateFormat sformat = new java.text.SimpleDateFormat();
			sformat.applyPattern("yyyy-MM");
			kmye.put("date", sformat.format(date));
			// 借贷方向
			String bd = list.get(0).get("balanceDirection");
			kmye.put("balanceDirection", bd);
			// 期初余额
			double ob = list.get(0).get("openingBalance");
			kmye.put("openingBalance", ob);
			// 借方发生额
			double jffs = list.get(0).get("jffs");
			kmye.put("jffs", jffs);
			// 贷方发生额
			double dffs = list.get(0).get("dffs");
			kmye.put("dffs", dffs);
			// 本年累计借方发生
			double ljjf = list.get(0).get("ljjf");
			kmye.put("ljjf", ljjf);
			// 本年累计贷方发生
			double ljdf = list.get(0).get("ljdf");
			kmye.put("ljdf", ljdf);
			array.add(0,kmye);

			List<Map<String, Object>> vis = new ArrayList<Map<String, Object>>();
			List<Voucheritem> vlist = Voucheritem.dao
					.find("SELECT voucherId,date,summary,debitAmount,creditAmount from ( SELECT vi.*,v.date FROM voucherItem vi LEFT JOIN voucher v ON v.voucherId=vi.voucherId ) as A WHERE date_format(date,'%Y-%m')='"
							+ date1 + "' and captionId = " + captionId + "  order by voucherId;");
			int length = vlist.size();
			// voucherItems
			for (int j = 0; j < length; j++) {
				Map<String, Object> vi = new HashMap<String, Object>();
				// item date
				java.sql.Date vdate = vlist.get(j).get("date");
				java.text.SimpleDateFormat format = new java.text.SimpleDateFormat();
				format.applyPattern("yyyy-MM-dd");
				vi.put("date", format.format(vdate));
				// item voucherId
				int voucherId = vlist.get(j).get("voucherId");
				vi.put("voucherId", voucherId);
				// item summary
				String summary = vlist.get(j).get("summary");
				vi.put("summary", summary);
				// item debitAmount(借方发生额)
				double debitAmount;
				if(vlist.get(j).get("debitAmount")==null){
					debitAmount = 0;
				}else{
					debitAmount = vlist.get(j).get("debitAmount");
				}
				vi.put("debitAmount", debitAmount);
				// item creditAmount(贷方发生额)
				double creditAmount;
				if(vlist.get(j).get("creditAmount")==null){
					creditAmount=0;
				}else{
					creditAmount = vlist.get(j).get("creditAmount");
				}
				vi.put("creditAmount", creditAmount);

				vis.add(j,vi);
			}

			array.add(1, vis);

			renderJson(array.toJSONString());
		}
	}
}
